package AdvanceStringDemo.demo01;

import java.util.Scanner;

//键盘录入一个字符串，长度小于等于9，只能是数字，将数字变成对应的罗马数字
//一 - I 二 - II 三 - III 四 - IV 五 - V 六 - VI 七 - VII 八 - VIII 九 - IX
//罗马数字里面没有0，字符串""代替
public class RomanConversion {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //定义变量在循环外面
        String s;
        while (true) {
            System.out.println("Enter a string: ");
             s = sc.nextLine();
            boolean flag = checkString(s);
            if (flag) {
                break;
            }else {
                System.out.println("字符串不符合规定，请重新输入");
                continue;
            }
        }
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //String src = changeLuoMa2(c);
            //sb.append(src);
            System.out.println(c);
            //根据Ascill字符与数字的差值来转换成整数
            int number = c - '0';
            sb.append(changeLuoMa(number)).append("  ");
        }
        System.out.println(sb.toString());
    }
    //查表法
    public static String changeLuoMa(int number) {
        String[] arr = {"","I", "II", "III", "IV", "Ⅴ", "VI", "VII", "VIII", "IX"};
        return arr[number];
    }
    //switch匹配方法
//    public static String changeLuoMa2(char number) {
//        String str = switch(number){
//            case '0' -> "";
//            case '1' -> "I";
//            case '2' -> "II";
//            case '3' -> "III";
//            case '4' -> "IⅤ";
//            case '5' -> "Ⅴ";
//            case '6' -> "ⅤI";
//            case '7' -> "VII";
//            case '8' -> "ⅤIII";
//            case '9' -> "IX";
//            default -> "";
//        };
//        return str;
//    }

    //检查字符串输入是否正确
    public static boolean checkString(String s) {
        if(s.length() > 9){
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c >= '0' && c <= '9'){
                continue;
            }else {
                return false;
            }
        }
        //只有把所以字符判断完毕才能返回true
        //不能放在循环体里面否则只能判断一个就返回
        return true;
    }
}
